package com.front.peikuahaitao.view.activity;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.front.peikuahaitao.R;
import com.front.peikuahaitao.base.BaseActivity;
import com.front.peikuahaitao.base.BasePresenter;
import com.front.peikuahaitao.utils.BottomTabManager;
import com.front.peikuahaitao.view.fragment.BagFragment;
import com.front.peikuahaitao.view.fragment.ClassificationFragment;
import com.front.peikuahaitao.view.fragment.HomeFragment;
import com.front.peikuahaitao.view.fragment.PersonalFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    private Context mContext = this;
    private boolean isChecked;
    private long time1;
    @BindView(R.id.linearLayout_main)
    LinearLayout linearLayoutMain;
    @BindView(R.id.radioButton_homepage)
    RadioButton radioButtonHomepage;
    @BindView(R.id.radioButton_goodtings)
    RadioButton radioButtonGoodtings;
    @BindView(R.id.radioButton_strategy)
    RadioButton radioButtonStrategy;
    @BindView(R.id.radioButton_my)
    RadioButton radioButtonMy;
    @BindView(R.id.radioGroup_main)
    RadioGroup radioGroupMain;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance());
        fragments.add(ClassificationFragment.newInstance());
        fragments.add(BagFragment.newInstance());
        fragments.add(PersonalFragment.newInstance());

        new BottomTabManager(
                fragments,
                radioGroupMain,
                getSupportFragmentManager(),
                R.id.linearLayout_main
        );
        Drawable drawableHomepage = getResources().getDrawable(R.drawable.home_selector);
        drawableHomepage.setBounds(0, 0, 80, 80);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        radioButtonHomepage.setCompoundDrawables(null, drawableHomepage, null, null);
        Drawable drawableClassification = getResources().getDrawable(R.drawable.category_selector);
        drawableClassification.setBounds(0, 0, 80, 80);
        radioButtonGoodtings.setCompoundDrawables(null, drawableClassification, null, null);
        Drawable drawableBag = getResources().getDrawable(R.drawable.shopping_selector);
        drawableBag.setBounds(0, 0, 80, 80);
        radioButtonStrategy.setCompoundDrawables(null, drawableBag, null, null);
        Drawable drawablePersonal = getResources().getDrawable(R.drawable.personal_selector);
        drawablePersonal.setBounds(0, 0, 80, 80);
        radioButtonMy.setCompoundDrawables(null, drawablePersonal, null, null);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (isChecked && System.currentTimeMillis() - time1 < 2000) {
                    finish();
                } else {
                    if (radioButtonHomepage.isChecked()) {
                        time1 = System.currentTimeMillis();

                        Toast toast = Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        isChecked = true;

                    } else {
                        radioButtonHomepage.setChecked(true);
                    }
                }
                break;
        }
        return false;
    }
}
